package com.lf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lf.constant.ConstantsValue;
import com.lf.entity.ResponseWrapper;
import com.lf.entity.SomeValEntity;
import com.lf.mapper.MapperList;
import com.lf.service.SysService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class SysServiceImpl extends MapperList implements SysService {

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void setRule(String key, String value) {
        SomeValEntity someValEntity = new SomeValEntity();
        someValEntity.setKey1(key);
        someValEntity = someValMapper.selectOne(someValEntity);
        if (someValEntity == null){
            someValEntity = new SomeValEntity();
            someValEntity.setKey1(key);
            someValEntity.setValue1(value);
            someValMapper.insert(someValEntity);
        }else{
            someValEntity.setValue1(value);
            someValMapper.updateByPrimaryKey(someValEntity);
        }

    }

    /**
     * 获取微信配置信息
     * @return
     */
    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public ResponseWrapper getWeiXinData() {
        SomeValEntity someValEntity = new SomeValEntity();
        someValEntity.setKey1("weixindata");
        someValEntity = someValMapper.selectOne(someValEntity);
        return ResponseWrapper.succeed(JSONObject.parseObject(someValEntity.getValue1()));
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public String getRule(String key) {
        SomeValEntity someValEntity = new SomeValEntity();
        someValEntity.setKey1(key);
        someValEntity = someValMapper.selectOne(someValEntity);
        if (someValEntity==null){
            return null;
        }
        return someValEntity.getValue1();
    }

    /**
     * 更新微信配置信息
     *
     * @param wx_appid
     * @param wx_secret
     * @param wx_notify
     * @param secret_id
     * @param secret_key
     * @return
     */
    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public ResponseWrapper updateWeiXinData(String wx_appid, String wx_secret, String wx_notify, String secret_id, String secret_key,String mch_id,String mch_key) {
        SomeValEntity someValEntity = new SomeValEntity();
        someValEntity.setKey1("weixindata");
        someValEntity = someValMapper.selectOne(someValEntity);

        JSONObject json = new JSONObject();
        json.put("WX_APPID",wx_appid);
        json.put("WX_SECRET",wx_secret);
        json.put("WX_NOTIFY",wx_notify);
        json.put("SECRET_ID",secret_id);
        json.put("SECRET_KEY",secret_key);
        json.put("MCH_ID",mch_id);
        json.put("MCH_KEY",mch_key);
        someValEntity.setValue1(json.toJSONString());

        someValMapper.updateByPrimaryKey(someValEntity);

        updateRedisWeixinData(json);

        return ResponseWrapper.succeed(JSONObject.parseObject(someValEntity.getValue1()));
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void initWeixinData() {
        SomeValEntity someValEntity = new SomeValEntity();
        someValEntity.setKey1("weixindata");
        JSONObject json = JSONObject.parseObject(someValMapper.selectOne(someValEntity).getValue1());
        updateRedisWeixinData(json);
    }

    public void updateRedisWeixinData(JSONObject json){
        redisService.setString(ConstantsValue.WX_COFIG + "WX_APPID",json.getString("WX_APPID"));
        redisService.setString(ConstantsValue.WX_COFIG + "WX_SECRET",json.getString("WX_SECRET"));
        redisService.setString(ConstantsValue.WX_COFIG + "WX_NOTIFY",json.getString("WX_NOTIFY"));
        redisService.setString(ConstantsValue.WX_COFIG + "SECRET_ID",json.getString("SECRET_ID"));
        redisService.setString(ConstantsValue.WX_COFIG + "SECRET_KEY",json.getString("SECRET_KEY"));
        redisService.setString(ConstantsValue.WX_COFIG + "MCH_ID",json.getString("MCH_ID"));
        redisService.setString(ConstantsValue.WX_COFIG + "MCH_KEY",json.getString("MCH_KEY"));
//        log.info("wxappid:{},wxsecret:{},wxnotify:{},secretid:{},secretkey:{}",
//                redisService.getString("WX_APPID"),
//                redisService.getString("WX_SECRET"),
//                redisService.getString("WX_NOTIFY"),
//                redisService.getString("SECRET_ID"),
//                redisService.getString("SECRET_KEY"));
    }
}
